package com.sxhuayuan.parking.entity;

import com.sxhuayuan.parking.config.InvoiceStatus;
import com.sxhuayuan.parking.utils.IdWorker;
import lombok.Getter;
import lombok.Setter;
import org.springframework.util.Assert;

import javax.persistence.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 发票记录表<br/>
 * 一条发票记录只能对应一次停车记录
 * 
 * @author helong
 * @create 2019-08-28 13:39
 */
@Getter
@Setter
@Entity
@Table(name = "hypk_invoice")
public class Invoice extends BaseEntity {
	private static final IdWorker idWorker = new IdWorker(1);
	private static final BigDecimal ONE = new BigDecimal(1);
	public Invoice() {
	}
	public Invoice(List<Order> orders, Long parkingLotId, BigDecimal ordertotal, BigDecimal taxRate, Member member, String pluginId) {
		Set<Order> set = new HashSet<>();
		for (Order order : orders) {
			order.setInvoiceStatus(InvoiceStatus.invoiceWait);
			set.add(order);
		}
		this.sn = idWorker.nextIdStr();
		this.orders = set;
		this.parkingLotId = parkingLotId;
		this.taxRate=taxRate;
		this.ordertotal = ordertotal;
		this.member = member;
		this.pluginId = pluginId;
		this.status=InvoiceStatus.invoiceWait;
		this.invoiceTime=new Date();
	}

	/** 发票开具状态,默认开票中 */
	private InvoiceStatus status = InvoiceStatus.invoiceWait;
	/** 发票流水号 */
	private String sn;
	/** 第三方发票流水号 */
	private String externalSn;
	/** 车场id */
	private Long parkingLotId;
	/** 开票时间 */
	private Date invoiceTime;
	/** 发票代码 */
	private String invoiceCode;
	/** 发票号码 */
	private String invoiceNum;
	/** 税率 */
	private BigDecimal taxRate;
	/** 订单合计金额(含税) */
	private BigDecimal ordertotal;
	/** 价税税额 元：保留两位小数 */
	private BigDecimal taxAmount;
	/** 不含税的金额 */
	private BigDecimal taxFreeAmount;
	/** 发票文件地址:存于第三方服务器 */
	private String fileUrl;
	/** 发票图片url:存于第三方服务器 */
	private String imgUrl;
	/** 购方用户账号 */
	private Member member;
	/** 发票校验码 */
	private String checkCode;
	/** 发票二维码 */
	private String qrCode;
	/** 税控设备号（机器编 码） */
	private String machineCode;
	/** 发票密文 */
	private String cipherText;
	/** 本次发票记录使用的插件 */
	private String pluginId;
	/** 错误信息 */
	private String errorMessage;
	/** 本次发票对应的订单列表 */
	private Set<Order> orders=new HashSet<>();

	@Column(nullable = false)
	public InvoiceStatus getStatus() {
		return status;
	}

	@Column(length = 50)
	public String getExternalSn() {
		return externalSn;
	}

	@Column(nullable = false)
	public Long getParkingLotId() {
		return parkingLotId;
	}

	@Column(nullable = false, precision = 12, scale = 2)
	public BigDecimal getTaxAmount() {
		return ordertotal.subtract(getTaxFreeAmount());
	}

	@Column(nullable = false, precision = 12, scale = 2)
	public BigDecimal getTaxFreeAmount() {
		Assert.notNull(ordertotal,"订单总金额不能为空");
		Assert.notNull(taxRate,"税率不能为空");
		return ordertotal.divide(ONE.add(taxRate),2, RoundingMode.HALF_UP);
	}

	@ManyToOne(fetch = FetchType.LAZY)
	public Member getMember() {
		return member;
	}

	public String getQrCode() {
		return qrCode;
	}

	@Column(nullable = false,length = 20)
	public String getPluginId() {
		return pluginId;
	}

	@Column(nullable = false, precision = 12, scale = 2)
	public BigDecimal getOrdertotal() {
		return ordertotal;
	}

	@OneToMany(fetch = FetchType.LAZY)
	public Set<Order> getOrders() {
		return orders;
	}

	@Column(nullable = false,length = 30)
	public String getSn() {
		return sn;
	}

	@Column(nullable = false, precision = 12, scale = 2)
	public BigDecimal getTaxRate() {
		return taxRate;
	}
}
